﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SaunaClient.Model.Network;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;

namespace SaunaClient.View
{
    /// <summary>
    /// Sauna Client Option Form
    /// </summary>
    /// <dates>
    /// 11/09/2011 - created by Chiaki Sato
    /// </dates>    
    public partial class ClientOptionForm : Form
    {
        public NetworkSetting networkSetting;
        private const string FTP_SSL = "FtpSsl";
        private const string CONFIG_FILE_NAME = @"SaunaConfig.ini";
        private const string ERROR_MESSAGE_INVALID_ENTRY = "Invalid Entry.";
        private const string ERROR_TITLE = "Error";

        /// <summary>
        /// Constructor
        /// </summary>
        public ClientOptionForm(NetworkSetting networkSetting)
        {

            InitializeComponent();

            this.networkSetting = networkSetting.Clone();

            textBoxFtpHostAddress.Text = this.networkSetting.FtpHostAddress;
            textBoxFtpUserName.Text = this.networkSetting.FtpUserName;
            textBoxFtpPassword.Text = this.networkSetting.FtpPassword;
            textBoxFtpServerDirectory.Text = this.networkSetting.FtpServerDirectory;
            textBoxFtpLocalDirectory.Text = this.networkSetting.FtpLocalDirectory;
            textBoxFtpPortNumber.Text = this.networkSetting.FtpPortNumber.ToString();
            checkBoxFtpSsl.Checked = this.networkSetting.FtpSsl;
            textBoxTcpHostAddress.Text = this.networkSetting.TcpHostAddress;
            textBoxTcpPortNumber.Text = this.networkSetting.TcpPortNumber.ToString();
            checkBoxTcpSsl.Checked = this.networkSetting.TcpSsl;
            textBoxLogFileDirectory.Text = this.networkSetting.LogFileDirectory;

        }

        /// <summary>
        /// OK button event handler
        /// </summary>
        /// <param name="sender">Event sender object</param>
        /// <param name="e">Event arguments</param>
        private void ButtonOk_Click(object sender, EventArgs e)
        {
            // OK Button
            try
            {
                networkSetting.FtpHostAddress = textBoxFtpHostAddress.Text;
                networkSetting.FtpUserName = textBoxFtpUserName.Text;
                networkSetting.FtpPassword = textBoxFtpPassword.Text;
                networkSetting.FtpServerDirectory = textBoxFtpServerDirectory.Text;
                networkSetting.FtpLocalDirectory = textBoxFtpLocalDirectory.Text;
                networkSetting.FtpSsl = checkBoxFtpSsl.Checked;
                networkSetting.TcpHostAddress = textBoxTcpHostAddress.Text;
                networkSetting.TcpSsl = checkBoxTcpSsl.Checked;
                networkSetting.LogFileDirectory = textBoxLogFileDirectory.Text;
                networkSetting.FtpPortNumber = int.Parse(textBoxFtpPortNumber.Text);
                networkSetting.TcpPortNumber = int.Parse(textBoxTcpPortNumber.Text);

                WriteToFile(CONFIG_FILE_NAME, networkSetting);

                DialogResult = DialogResult.OK;

                Close();
            }
            catch
            {
                MessageBox.Show(ERROR_MESSAGE_INVALID_ENTRY, ERROR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        /// <summary>
        /// Write ftp base setting to file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="obj"></param>
        public void WriteToFile(string filePath, object obj)
        {
            using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, obj);
            }
        }

        /// <summary>
        /// Read ftp base setting from file
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public object ReadFromFile(string filePath)
        {
            object obj = null;
            try
            {
                using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    IFormatter formatter = new BinaryFormatter();
                    obj = formatter.Deserialize(stream);
                }
            }
            catch (FileNotFoundException ex)
            {
                // Write error.
                Console.WriteLine(ex);
            }
            return obj;
        }
    }
}
